<template>
	<collect-btn :changeCollectStuta="changeCollectStuta" :flag="myCollect"></collect-btn>
</template>

<script>
	import  {MessageBox} from '../../../ui-components'
	import {insertCollect,deleteCollect,queryCollect} from '../../../api/zxApi.js';
	import CollectBtn from "../../../real-components/collectBtn";
	import {mapGetters,mapMutations} from "vuex";
	import {getPageTree, initDomTree} from "../../../utils/getPageTree";
	import {Toast} from '../../../utils/toast';
	import axios from "axios";
	export default {
		props:{
			//实际为goodsCode
			goodsId: {
            	type: String,
                default: function(){
                	return ''
                }
           },
           isShare: {
           		default: 0
           }
		},
		data(){
			return {
				collect:false,
				collectIdInfor:null
			}
		},
		components: {
			"collect-btn": CollectBtn
		},
		computed:{
			...mapGetters('wxh',[
				'sessionId',
				'userInfo'
			]),
			myCollect(){
				console.log(888,this.collect)
				return this.collect;
			}
		},
		watch:{
			collect(){
				
				console.log(this.collect,'collect')
//				return this.collect;
			},
			goodsId(){
				console.log("调用了",this.goodsId)
				this.getCollectData();
			}
		},
		created() {
//			console.log(this.goodsId,'this.goodsId---------------------')
		},
		methods: {
			getCollectData(){
				var that = this;
				this.collect = false;
			    var sessionId = this.sessionId;
			    if (this.sessionId) {
			    	let params = {
			        	storeCode:localStorage.getItem("storeCode")
			        }
			        //请求数据
			        axios({
			            url: window.api.goods_host + "/collection/goods",
			            method: "get",
			            params,
			            headers: {"sessionId": sessionId},
			          }
			        ).then((data) => {
			          var data = data.data.data;
			          //获取全部商品数据的方法
			          that.collectIdInfor = data;

			          for(let i = 0 ; i < that.collectIdInfor.length;i++){
//			          	console.log(that.collectIdInfor[i].goodsCode,that.goodsId,that.collectIdInfor[i].goodsCode==that.goodsId,'比较两个id')
			          	if(JSON.stringify(that.collectIdInfor[i].goodsCode) == that.goodsId){
							console.log(that.collectIdInfor,that.collectIdInfor[i].goodsCod,that.goodsId,'123')
							that.collect = true;
						}
			          	
			          }
			        }).catch(
			          this.collectIdInfor = null
			        )
			    } else {
			        //弹出未登录提示 点击返回上一页
			    }
			},
			changeCollectStuta () {
				if(this.isShare) {
			      	this.$router.push({
						name: "MiddleWare",
						})
		            return
				}
		        var that = this
		       
		        if (!this.sessionId) {
			        MessageBox.alert('请登录后再收藏', '操作失败').then(
			            () => {
			              this.$router.push({path: "login", query: {"page": "/detail/" + this.goodCode}})
			            }
			        )
			          return
		        }
		        this.collect = !this.collect;
		        if (this.collect) {
		        	var goodsCode = JSON.parse(this.goodsId);
			        axios({
			            headers: {"sessionId": that.sessionId},
			            url: window.api.goods_host + "/collection/goods",
			            method: "post",
			            data: {
			            	"storeCode":localStorage.getItem('storeCode'),
							"goodsCodeStr":goodsCode
						},
			        }).then((res) => {
                        
                        console.log(res.data.returnCode,'res.data.returnCode');
                        if(res.data.returnCode == 1){
                            this.flag = true;
                            Toast({
                                message: "收藏成功",
                                type: 'success'
                            });
                        }else{
                            Toast({
                                message: "收藏失败",
                                type: 'fail'
                            });
                        }
			            
			        })
		        } else {
		        	let params = {
				        storeCode:localStorage.getItem('storeCode'),
				        goodsCodeStr:JSON.parse(that.goodsId)
				    }
			        axios({
			           url: window.api.goods_host + "/collection/goods",
			           headers: {"sessionId": that.sessionId},
			           method: "DELETE",
			           params
			        }).then((res) => {
                        if(res.data.returnCode == 1){
                            this.flag = false;
                            Toast({
                                message: "移除成功",
                                type: 'success'
                            });
                        }else{
                            Toast({
                                message: "移除失败",
                                type: 'fail'
                            });
                        }
			        })
		        }
		
		    },
		}
		
	}
</script>

<style scoped="scoped">
	
</style>